import * as staffService from '@/services/staff';
import { message } from 'antd';
import { DEFAULT_RESPONE_SUCCESS } from '@/constants';

export default {
  namespace: 'staff',

  state: {
    current: 1,
    total: 0,
    data: [],
    rowKey: 'id'
  },

  subscriptions: {

  },

  effects: {
    *queryData({ payload }, { call, put }) {
      const { callback, method = 'queryData', current = 1, pageSize, unit_id, ...extra } = payload;

      const response = yield call(staffService[method], { pageNo: current, pageSize, unitId: typeof unit_id === 'object' ? (unit_id.id === -1 ? undefined : unit_id.id) : unit_id === -1 ? undefined : unit_id, ...extra });
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result = {} } = response;
        const { data = [], total } = result;

        yield put({
          type: 'changeState',
          payload: {
            data,
            current,
            pageSize,
            total: typeof total === 'string' ? parseInt(total) : total
          }
        });
      }
    },
    *saveOrUpdateData({ payload }, { call, put }) {
      const { data, ...extra } = payload;
      const option = {
        method: 'POST',
        body: data
      };
      let response = {};
      if (!data.id) {
        response = yield call(staffService.saveData, option);
      } else {
        response = yield call(staffService.updateData, option);
      }
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success(!data.id ? '新增人员成功' : '编辑人员成功', 1);
        yield put({
          type: 'changeState',
          payload: { visible: false, editData: {} }
        });
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *deleteData({ payload }, { call, put }) {
      const { ids, ...extra } = payload;
      const option = {
        method: 'POST',
        body: ids
      };
      const response = yield call(staffService.deleteData, option);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        message.success('停用人员成功', 1);
        yield put({ type: 'queryData', payload: { ...extra } });
      }
    },
    *downloadData({ payload }, { call, put }) {
      try {
        const { name, type, ...extra } = payload;

        const response = yield call(staffService.downloadData, { type }, { ...extra });
        if (response) {
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(response, name);
          } else {
            const URL = window.URL || window.webkitURL;
            const downloadUrl = URL.createObjectURL(response);
            const a = document.createElement('a');

            if (typeof a.download === 'undefined') {
              window.location = downloadUrl;
            } else {
              a.download = name;
              a.href = downloadUrl;
              a.style.display = 'none';
              document.body.appendChild(a);
              a.click();
              window.URL.revokeObjectURL(downloadUrl);
              document.body.removeChild(a);
            }
          }
        } else {
          message.error('下载失败！');
        }
      } catch (e) {
        console.error(e);
      }
    },
    *uploadData({ payload }, { call, put }) {
      const { callback, unitId, optType, file } = payload;
      const formData = new FormData();
      formData.append('file', file);

      const response = yield call(staffService.uploadData, { unitId, optType }, { method: 'POST', body: formData });
      if (response) {
        const { descr, result } = response;

        if (typeof callback === 'function') {
          callback(descr === '' ? '200' : '400', result, descr);
        }
      }
    },
    *getTelephone({ payload }, { call, put }) {
      const { callback, ...extra } = payload;

      const response = yield call(staffService.getTelephone, extra);
      if (response && response.status === DEFAULT_RESPONE_SUCCESS) {
        const { result } = response;

        if (typeof callback === 'function') {
          callback(result);
        }
      }
    }
  },

  reducers: {
    clear(state, { payload }) {
      return {
        current: 1,
        total: 0,
        data: [],
        rowKey: 'id'
      };
    },
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  },
};
